/* 
 * Copyright (c), nFractals Ltd. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.parade.distributed;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.parade.Constant;
import org.parade.driver.Driver;

public class Condition implements java.util.concurrent.locks.Condition, Object{
	private Driver driver;
	private Lock lock;
	private java.util.concurrent.locks.Condition condition;

	/**
	 * Create condition from given lock
	 * 
	 * @param driver
	 * @param lock
	 */

	public Condition(Driver driver, Lock lock){
		this.driver = driver;
		this.lock = lock;
		this.condition = driver.getCondition(lock);
		
	}

	@Override
	public void await() throws InterruptedException {
		condition.await();
	}

	@Override
	public boolean await(long time, TimeUnit unit) throws InterruptedException {
		return condition.await(time, unit);
	}

	@Override
	public long awaitNanos(long nanosTimeout) throws InterruptedException {
		return condition.awaitNanos(nanosTimeout);
	}

	@Override
	public void awaitUninterruptibly() {
		condition.awaitUninterruptibly();
		
	}

	@Override
	public boolean awaitUntil(Date deadline) throws InterruptedException {
		return condition.awaitUntil(deadline);
	}

	@Override
	public void signal() {
		condition.signal();
		
	}

	@Override
	public void signalAll() {
		condition.signalAll();
	}

	@Override
	public Id getDistributedObjectId() {
		return lock.generateConditionObjectId();
	}
}
